<!--订单分类占比图-->
<template>
    <div class="com-container">
        <div class="com-chart" ref="hot_ref"></div>
        <span class="iconfont arr-left" @click="toLeft" :style="comStyle">&#xe6ef;</span>
        <span class="iconfont arr-right"@click="toRight" :style="comStyle">&#xe6ed;</span>
        <span class="cat-name" :style="comStyle">{{catName}}</span>
    </div>
</template>

<script>
    import {mapState} from "vuex";
    import {getThemeValue} from '../utils/theme_utils'
    export default {
        name: "Hot",
        data() {
            return {
                chartInstance:null,
                allData:null,
                currentIndex:0,//当前所展示的占比图的数据
                titleFontSize:0,


                //testTime:null,
            }
        },
        computed:{
            catName(){
                if (this.allData==null){
                    return ''
                }else {
                    return this.allData[this.currentIndex].name
                }
            },
            comStyle() {
                return {
                    fontSize:this.titleFontSize+'px',
                    color:getThemeValue(this.theme).titleColor,
                };
            },
            ...mapState(['theme'])

        },
        created(){
            //注册回调函数
            this.$socket.registerCallBack("hotData",this.getData);
        },
        mounted(){
            this.initChart();
            //this.getData();
            //发送数据给服务器，告诉服务器，我现在需要数据
            this.$socket.send({
                action:'getData',
                socketType:'hotData',
                chartName:'hot',
                value:''
            });
            window.addEventListener('resize',this.screenAdapter);
            //在页面加载完成忠厚，主动进行屏幕的适配
            this.screenAdapter();
        },
        destroy(){
            //在组件销毁的时候，将窗口监听器取消掉
            window.removeEventListener('resize',this.screenAdapter);
            this.$socket.unRegisterCallBack("hotData")
        },
        methods: {
            //初始化echartInstance对象
            initChart(){
                this.chartInstance=this.echarts.init(this.$refs.hot_ref,this.theme);
                const initOption={
                    title:{
                        text:'┃ 订单分类占比',
                        left:20,
                        top:20,
                    },
                    legend:{
                        top:'15%',
                        icon:'circle',
                    },
                    tooltip:{
                        show:true,
                        formatter:(arg)=>{
                            if (this.currentIndex==1){
                                return arg.name+":00点 : "+arg.percent+" %"
                            }
                            return arg.name+" : "+arg.percent+" %"
                        }
                    },
                    series:[
                        {
                            type:'pie',
                            label:{
                                show:false
                            },
                            emphasis:{
                                label:{
                                    show:true
                                },
                                labelLine:{
                                    show:false
                                }
                            }
                        }
                    ]
                };
                this.chartInstance.setOption(initOption);
            },
            //获取服务器数据
            async getData(ret){
               /* await this.axios.get("/chart/hot").then(res=>{
                    this.allData=res.data;
                });*/
                this.allData=ret;
                this.updateChart();
                //this.startTestTime();
            },
            //处理数据
            updateChart(){
                const legendData=this.allData[this.currentIndex].children.map(item=>{
                    return item.name;
                });
                const seriesData=this.allData[this.currentIndex].children.map(item=>{
                   return{
                       name:item.name,
                       value:item.value,
                   }
                });
                const dataOption={
                    legend:{
                        data:legendData,
                    },
                    series:[
                        {
                            data:seriesData
                        }
                    ]
                };
                this.chartInstance.setOption(dataOption);
            },
            //当浏览器的大小发生变化的时候，调用该方法，来完成屏幕的适配
            screenAdapter(){
                this.titleFontSize=this.$refs.hot_ref.offsetWidth/100*3.6;
                //和分辨率大小相关的配置项
                const adapterOption={
                    title:{
                        textStyle:{
                            fontSize:this.titleFontSize
                        }
                    },
                    legend:{
                        itemWidth:this.titleFontSize,
                        itemHeight:this.titleFontSize,
                        itemGap:this.titleFontSize/2,
                        textStyle:{
                            fontSize:this.titleFontSize/2
                        }
                    },
                    series:[
                        {
                            radius:this.titleFontSize * 4.5,
                            center:['50%','60%']
                        }
                    ]
                };
                this.chartInstance.setOption(adapterOption);
                //手动的调用图表的resize方法
                this.chartInstance.resize();
            },
            toLeft(){
                this.currentIndex--;
                if (this.currentIndex < 0) {
                    this.currentIndex=this.allData.length-1;
                }
                this.updateChart();
            },
            toRight(){
                this.currentIndex++;
                if (this.currentIndex > this.allData.length-1) {
                    this.currentIndex=0;
                }
                this.updateChart();
            },
            /*startTestTime(){
                this.testTime=setInterval(()=>{
                    this.axios.get("/chart/hot").then(res=>{
                        this.allData=res.data;
                    });
                    this.updateChart();
                },1000);
            }*/
        },
        watch:{
            theme(){
                //console.log("主题切换");
                this.chartInstance.dispose();//销毁当前图表
                this.initChart();//以切换后的主题显示图表
                this.screenAdapter();//完成屏幕适配
                this.updateChart();//更新图表的展示
            }
        }
    }

</script>

<style scoped>
    .com-container{
        width: 100%;
        height: 100%;
        overflow: hidden;
        position: relative;
    }
    .com-chart{
        width: 100%;
        height: 100%;
        overflow: hidden;
        border-radius: 20px;
    }
    .arr-left{
        position: absolute;
        left: 10%;
        top:50%;
        transform: translateY(-50%);
        cursor: pointer;
        color: white;
    }
    .arr-right{
        position: absolute;
        right: 10%;
        top:50%;
        transform: translateY(-50%);
        cursor: pointer;
        color: white;
    }
    .cat-name{
        position: absolute;
        left: 80%;
        bottom: 20px;
        color: white;
    }
</style>
